import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import "package:get/get.dart";
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../common/my_icon.dart';
import '../../../common/style.dart';
import '../../../components/appbar_widget/appbar_widget.dart';
import '../../../components/loading/svg_loading.dart';
import '../../../components/status/empty.dart';
import '../../../components/status/load_more_state.dart';
import '../../../components/status/not_network.dart';
import '../../../config/global_config.dart';
import '../../../controller/network/network_ctrl.dart';
import '../../../model/bean/community_bean.dart';
import '../../../utils/screenutil/fit.dart';
import 'ctrl/repairs_list_ctrl.dart';

class RepairsList extends StatelessWidget {
  static const _COMPLETE = "已维修";
  static const _END = "未维修";

  // 导入控制器
  final rlCtrl = Get.put(RepairsListCtrl());
  final _nc = Get.find<NetworkCtrl>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget("报修待办").build(context),
      body: Obx(
        () => rlCtrl.isInitComplete.value
            ? Container(
                height: double.infinity,
                child: Stack(
                  children: [
                    Container(
                      margin: EdgeInsets.only(
                          top: fit(_nc.topDistance.value + 150)),
                      child: rlCtrl.MARKSYMBOL.value
                          ? SmartRefresher(
                              footer: CustomFooter(
                                builder: (BuildContext ct, LoadStatus mode) {
                                  Widget body = LoadMoreState(mode);
                                  return Container(child: Center(child: body));
                                },
                              ),
                              enablePullDown: false,
                              enablePullUp: true,
                              controller: rlCtrl.refreshController.value,
                              onLoading: rlCtrl.onLoading,
                              child: ListView.builder(
                                itemCount: rlCtrl.listData.length,
                                itemBuilder: (BuildContext c, int i) => InkWell(
                                  onTap: () => rlCtrl.onLeave(
                                    arg: {
                                      "id": rlCtrl.listData[i].id,
                                      "type": rlCtrl.status.value,
                                    },
                                  ),
                                  child: Container(
                                    color: Colors.white,
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.end,
                                      children: [
                                        Container(
                                          padding: EdgeInsets.only(
                                            left: 20.f,
                                            top: 20.f,
                                            bottom: 20.f,
                                          ),
                                          child: Row(
                                            children: [
                                              Container(
                                                width: 570.f,
                                                child: Column(
                                                  crossAxisAlignment:
                                                      CrossAxisAlignment.start,
                                                  children: [
                                                    Text(
                                                      "${rlCtrl.listData[i].content}",
                                                      style: TextStyle(
                                                        fontSize: 24.f,
                                                      ),
                                                      textAlign: TextAlign.left,
                                                      maxLines: 1,
                                                      overflow:
                                                          TextOverflow.ellipsis,
                                                    ),
                                                    Container(
                                                      height: 30.f,
                                                      margin: EdgeInsets.only(
                                                          top: 5.f),
                                                      alignment:
                                                          Alignment.center,
                                                      child: Row(
                                                        mainAxisAlignment:
                                                            MainAxisAlignment
                                                                .spaceBetween,
                                                        crossAxisAlignment:
                                                            CrossAxisAlignment
                                                                .end,
                                                        children: [
                                                          Container(
                                                            width: 350.f,
                                                            child: Text(
                                                              "${rlCtrl.listData[i].buildingName}",
                                                              textAlign:
                                                                  TextAlign
                                                                      .left,
                                                              maxLines: 1,
                                                              overflow:
                                                                  TextOverflow
                                                                      .ellipsis,
                                                              style: TextStyle(
                                                                fontSize: 22.f,
                                                                color: GlobalConfig
                                                                    .assistColor,
                                                              ),
                                                            ),
                                                          ),
                                                          Container(
                                                            width: 210.f,
                                                            alignment: Alignment
                                                                .centerRight,
                                                            child: Text(
                                                              "${rlCtrl.listData[i].createTime}",
                                                              style: TextStyle(
                                                                fontSize: 22.f,
                                                                color: GlobalConfig
                                                                    .assistColor,
                                                              ),
                                                            ),
                                                          ),
                                                        ],
                                                      ),
                                                    )
                                                  ],
                                                ),
                                              ),
                                              Container(
                                                child: Icon(
                                                  Icons.chevron_right,
                                                  size: 35.f,
                                                  color:
                                                      GlobalConfig.assistColor,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        SizedBox(
                                          width: rlCtrl.listData.length - 1 == i
                                              ? 640.f
                                              : 620.f,
                                          height: 1.f,
                                          child: Container(
                                              color: GlobalConfig.borderColor),
                                        )
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            )
                          : Container(
                              width: 640.f,
                              height: double.infinity,
                              child: Center(child: Empty()),
                            ),
                    ),

                    // 选项卡、条件选择器
                    Positioned(
                      child: Container(
                        width: 640.f,
                        height: 150.f,
                        margin: EdgeInsets.only(
                          top: fit(_nc.topDistance.value),
                        ),
                        decoration: BoxDecoration(
                          boxShadow: GlobalConfig.boxShadow,
                        ),
                        child: Column(
                          children: [
                            Container(
                              width: 640.f,
                              height: 85.f,
                              color: Colors.white,
                              child: Flex(
                                direction: Axis.horizontal,
                                children: [
                                  Expanded(
                                    flex: 1,
                                    child: createTabs(_END, 0),
                                  ),
                                  SizedBox(
                                    width: 1.f,
                                    child: Container(
                                      height: 60.f,
                                      decoration: BoxDecoration(
                                        gradient: Style.gradient,
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: createTabs(_COMPLETE, 1),
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              width: 640.f,
                              decoration: BoxDecoration(
                                color: Colors.white,
                                border: Border(
                                  top: BorderSide(
                                    width: 1.f,
                                    color: GlobalConfig.borderColor,
                                  ),
                                ),
                              ),
                              child: Row(
                                children: [
                                  Expanded(
                                    child: InkWell(
                                      onTap: () => rlCtrl.getDateRange(context),
                                      child: Container(
                                        width: 319.f,
                                        height: 64.f,
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.end,
                                          children: [
                                            Container(
                                              padding: EdgeInsets.only(
                                                  left: rlCtrl.selectedDateRange
                                                              .value ==
                                                          "请选择时间"
                                                      ? 20.f
                                                      : 5.f),
                                              width: 255.f,
                                              child: Text(
                                                "${rlCtrl.selectedDateRange.value}",
                                                maxLines: 1,
                                                textAlign: TextAlign.left,
                                                overflow: TextOverflow.ellipsis,
                                                style: TextStyle(
                                                  color: rlCtrl
                                                              .selectedDateRange
                                                              .value ==
                                                          "请选择时间"
                                                      ? GlobalConfig.assistColor
                                                      : Colors.black,
                                                  fontSize: rlCtrl
                                                              .selectedDateRange
                                                              .value ==
                                                          "请选择时间"
                                                      ? 22.f
                                                      : 20.f,
                                                ),
                                              ),
                                            ),
                                            InkWell(
                                              onTap: () =>
                                                  rlCtrl.emptyDateRange(),
                                              child: Container(
                                                width: 64.f,
                                                height: 64.f,
                                                // color: Colors.green,
                                                child: Icon(
                                                  rlCtrl.selectedDateRange
                                                              .value ==
                                                          "请选择时间"
                                                      ? Icons.chevron_right
                                                      : MyIcon.cuowu,
                                                  color:
                                                      GlobalConfig.assistColor,
                                                  size: 32.f,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ),
                                  SizedBox(
                                    width: 1.f,
                                    height: 64.f,
                                    child: Container(
                                      color: GlobalConfig.borderColor,
                                    ),
                                  ),
                                  Expanded(
                                    child: Container(
                                      width: 319.f,
                                      height: 64.f,
                                      alignment: Alignment.centerLeft,
                                      padding: EdgeInsets.only(left: 20.f),
                                      child: DropdownButtonHideUnderline(
                                        child: DropdownButton(
                                          isExpanded: true,
                                          items: rlCtrl.communityData.value,
                                          value: rlCtrl.selectedCommunity.value,
                                          hint: Container(
                                            alignment: Alignment.centerLeft,
                                            child: Text(
                                              "${rlCtrl.selectedCommunity.value != null ? rlCtrl.selectedCommunity.value.name : '请选择小区'}",
                                              // softWrap: true,
                                              maxLines: 1,
                                              textAlign: TextAlign.right,
                                              overflow: TextOverflow.ellipsis,
                                              style: TextStyle(
                                                color: GlobalConfig.assistColor,
                                                fontSize: 22.f,
                                              ),
                                            ),
                                          ),
                                          icon: Container(
                                            width: 64.f,
                                            height: 64.f,
                                            child: Icon(
                                              Icons.chevron_right,
                                              size: 32.f,
                                              color: GlobalConfig.assistColor,
                                            ),
                                          ),
                                          onChanged: (CommunityBean val) =>
                                              rlCtrl
                                                  .setDropDownSelectedData(val),
                                        ),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                    ),

                    // 网络状态
                    Positioned(top: 0, child: NotNetwork()),
                  ],
                ),
              )
            : SVGLoading(),
      ),
    );
  }

  /// 创建头部选项卡
  /// * [name] String 选项卡名称|必填
  /// * [currentStatus] MaintenanceStatus 当前状态|必填
  Widget createTabs(String name, int currentStatus) {
    return InkWell(
      onTap: () => rlCtrl.onChangeSelectStatus(currentStatus),
      child: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 140.f,
              height: 85.f,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                    width: 2,
                    color: rlCtrl.status.value == currentStatus
                        ? GlobalConfig.theme
                        : Colors.white,
                  ),
                ),
              ),
              child: Text(
                "$name",
                style: TextStyle(
                  fontSize: 26.f,
                  color: rlCtrl.status.value == currentStatus
                      ? GlobalConfig.theme
                      : GlobalConfig.assistColor,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

/*

Container(
                              width: 640.f,
                              decoration: BoxDecoration(
                                color: Colors.white,
                                border: Border(
                                  top: BorderSide(
                                    width: 1.f,
                                    color: GlobalConfig.borderColor,
                                  ),
                                ),
                              ),
                              child: Column(
                                children: [
                                  Container(
                                    height: 64.f,
                                    padding: EdgeInsets.only(left: 20.f),
                                    child: InkWell(
                                      onTap: () => rlCtrl.getDateRange(context),
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          Text(
                                            "时间：",
                                            style: TextStyle(fontSize: 22.f),
                                          ),
                                          Expanded(
                                            child: Container(
                                              //color: Colors.red,
                                              alignment: Alignment.centerRight,
                                              padding:
                                                  EdgeInsets.only(left: 15.f),
                                              //width: 275.f,
                                              child: Text(
                                                rlCtrl.selectedDateRange.value,
                                                softWrap: true,
                                                maxLines: 1,
                                                textAlign: TextAlign.right,
                                                overflow: TextOverflow.ellipsis,
                                                style: TextStyle(
                                                  color: rlCtrl
                                                              .selectedDateRange
                                                              .value ==
                                                          "请选择时间"
                                                      ? GlobalConfig.assistColor
                                                      : Colors.black,
                                                  fontSize: 22.f,
                                                ),
                                              ),
                                            ),
                                          ),
                                          InkWell(
                                            onTap: () =>
                                                rlCtrl.emptyDateRange(),
                                            child: Container(
                                              width: 64.f,
                                              height: 64.f,
                                              child: Icon(
                                                rlCtrl.selectedDateRange
                                                            .value ==
                                                        "请选择时间"
                                                    ? Icons.chevron_right
                                                    : MyIcon.cuowu,
                                                color: GlobalConfig.assistColor,
                                                size: 32.f,
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                  SizedBox(
                                    width: 640.f,
                                    height: 1.f,
                                    child: Container(
                                        color: GlobalConfig.borderColor),
                                  ),
                                  Container(
                                    height: 64.f,
                                    padding: EdgeInsets.only(left: 20.f),
                                    child: InkWell(
                                      onTap: () => null,
                                      child: Row(
                                        children: [
                                          Text(
                                            "小区名称：",
                                            style: TextStyle(fontSize: 22.f),
                                          ),
                                          Expanded(
                                            child: Container(
                                              alignment: Alignment.centerRight,
                                              padding: EdgeInsets.only(
                                                left: 20.f,
                                              ),
                                              child:
                                                  DropdownButtonHideUnderline(
                                                child: DropdownButton(
                                                  isExpanded: true,
                                                  items: rlCtrl
                                                      .communityData.value,
                                                  value: rlCtrl
                                                      .selectedCommunity.value,
                                                  hint: Container(
                                                    width: 425.f,
                                                    alignment:
                                                        Alignment.centerRight,
                                                    color: Colors.yellow,
                                                    child: Text(
                                                      "${rlCtrl.selectedCommunity.value != null ? rlCtrl.selectedCommunity.value.name : '请选择小区'}",
                                                      // softWrap: true,
                                                      maxLines: 1,
                                                      textAlign:
                                                          TextAlign.right,
                                                      overflow:
                                                          TextOverflow.ellipsis,
                                                      style: TextStyle(
                                                        color: GlobalConfig
                                                            .assistColor,
                                                        fontSize: 22.f,
                                                      ),
                                                    ),
                                                  ),
                                                  icon: Container(
                                                    width: 64.f,
                                                    height: 64.f,
                                                    child: Icon(
                                                      Icons.chevron_right,
                                                      size: 32.f,
                                                      color: GlobalConfig
                                                          .assistColor,
                                                    ),
                                                  ),
                                                  onChanged: (CommunityBean
                                                          val) =>
                                                      rlCtrl
                                                          .setDropDownSelectedData(
                                                              val),
                                                ),
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            )

 */
